import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.0
import AIClient.module 1.0
Page {
    signal editDataset(string uid, string dataset_name, string dataset_description)
    signal createDataset(string dataset_name, string dataset_path, string dataset_description, int pb)
    signal cancel()
    function updateMode() {
        if ( m_mode === 0 )
        {
            dataset_path.visible = false
            dataset_path_tip.visible = false
            dataset_path_browse.visible = false
            dataset_description.enabled = true
            dataset_name.enabled = true
            btn.visible = true
            publicCombo.visible = false
            publicCombo.enabled = false
            dataset_public_tip.visible = false
            update()
        }
        else if ( m_mode === 1 )
        {
            dataset_path.visible = true
            dataset_path_tip.visible = true
            dataset_path_browse.visible = true
            dataset_description.enabled = true
            dataset_name.enabled = true
            publicCombo.visible = true
            publicCombo.enabled = true
            dataset_public_tip.visible = true
            btn.visible = true
        }
        else if ( m_mode === 2 )
        {
            dataset_path.visible = false
            dataset_path_tip.visible = false
            dataset_path_browse.visible = false

            dataset_description.enabled = false
            dataset_name.enabled = false
            publicCombo.visible = true
            publicCombo.enabled = false
            dataset_public_tip.visible = true
            btn.visible = false
            update()
        }
    }

    FileDialog {
        id: fileDialog
        visible: false
        title: "Please choose a file"
        folder: shortcuts.home
        modality: Qt.WindowModal
        onAccepted: {
            //visible = true
            var filePath = fileDialog.fileUrl.toString()

            dataset_path.text = filePath.substring(7,filePath.length)
            console.log("You chose: " + fileDialog.fileUrls)
            //visible = false
            fileDialog.close()
        }
        onRejected: {
            console.log("Canceled")
//            Qt.quit()
        }
        //Component.onCompleted: visible = true
    }
    ListModel {
        /* 数据项 */
        id: publicModel
        ListElement {
            pb: 0;
            text: "否";
            color: "black";
        }
        ListElement {
            pb: 1;
            text: "是";
            color: "black";
        }
    }
//    AIClient {
//        id: client
//    }

//    TipMessage {
//        id: tip
//        width: parent.width
//    }

//    function setToken(token) {
//        m_token = token
//    }

    id: dataset_create
    title: "创建数据集"
    visible: false
    //visibility: "Maximized"
//    modality: Qt.ApplicationModal
//    minimumWidth: Screen.desktopAvailableWidth*0.3
//    maximumWidth: Screen.desktopAvailableWidth
//    minimumHeight: Screen.desktopAvailableHeight*0.6
//    maximumHeight: Screen.desktopAvailableHeight
    width: parent.width*0.8
    height: parent.height*0.8

    property int m_mode: 1
    //property string m_token: ""
    property string m_uid: ""
    property string m_dataset_name: ""
    property string m_dataset_description: ""
    property string m_dataset_path: ""
    property int m_pb: 0
    property string m_btn: ""
    //property int m_from: 0
    //color: "azure"

//    Flickable {
//        id: flickable
//        clip: true
//        anchors.fill: parent
//        Keys.onUpPressed: scrollBar.decrease()
//            Keys.onDownPressed: scrollBar.increase()
//        // ...

    Rectangle {
        anchors.centerIn: parent
        color:"azure"
        width: dataset_create.width
        height: dataset_create.height

        GridLayout {
            id: grid_dataset_create
            rows: 5
            columns: 3
            anchors.topMargin: 100
            anchors.bottomMargin: 100
            anchors.leftMargin: 100
            anchors.rightMargin: 100
            anchors.fill: parent
            columnSpacing: 5
            rowSpacing: 15
            Text {
                text: qsTr("名称")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.maximumWidth: 200
                Layout.preferredWidth: 180
                color: "black"
                Layout.column: 0
                Layout.row: 0
            }
            TextField {
                id: dataset_name
                text: m_dataset_name
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*1.5
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                Layout.fillWidth: true
                Layout.row: 0
                Layout.column: 1
            }
            Rectangle {
                Layout.row: 0
                Layout.column: 2
//                Layout.minimumHeight: 200
                Layout.fillHeight: false
                Layout.fillWidth: true
                color: "transparent"
            }
            Text {
                text: qsTr("数据集描述")
                font.pointSize: 12
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.row: 1
                Layout.column: 0
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 400
                Layout.minimumHeight: 12*7
                Layout.maximumHeight: 400
                //height: 500
                //Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 1
                Layout.column: 1
                id: dataset_description
            }

//            ScrollView{
//                //Layout.minimumWidth: 500
//                Layout.maximumHeight: 400
//                Layout.preferredHeight: 400
//                Layout.minimumHeight: 12*5
//                Layout.fillWidth: true
//                Layout.row: 1
//                Layout.column: 1
//                TextArea {
//                    id: dataset_description
//                    text: m_dataset_description
//                    selectByMouse: true
//                    font.pointSize: 12

//                    background: Rectangle{
//                        anchors.fill: parent
//                        width: parent.width
//                        color: "white"
//                        border.color: "gray"
//                    }
//                }
//            }
//            TextField {
//                id: dataset_description
//                text: m_dataset_description
//    //            passwordCharacter: "*"
//                Layout.preferredHeight: 200
//                Layout.preferredWidth: 500
//                Layout.fillHeight: true
//                Layout.fillWidth: true
//                Layout.row: 1
//                Layout.column: 1
//            }
            Text {
                text: qsTr("数据集路径")
                id: dataset_path_tip
                font.pointSize: 12
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.row: 2
                Layout.column: 0
                color: "black"
            }
            TextField {
                id: dataset_path
                text: m_dataset_path
                selectByMouse: true
    //            passwordCharacter: "*"
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.minimumHeight: 12*1.5
                Layout.maximumHeight: 60
                Layout.fillWidth: true
                Layout.row: 2
                Layout.column: 1
            }
            OrangeButton {
                text:"浏览"
                id: dataset_path_browse
                Layout.maximumHeight: 60
                Layout.fillWidth: true
                Layout.preferredWidth: 40
                Layout.row: 2
                Layout.column: 2
                onClicked:  {
                    fileDialog.open()
                }
            }
            Text {
                text: qsTr("数据集公开")
                id: dataset_public_tip
                font.pointSize: 12
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.row: 3
                Layout.column: 0
                color: "black"
            }
            ComboBox {
                id: publicCombo
                currentIndex: 1
                Layout.column: 1
                Layout.row: 3
                font.pointSize: 12
                Layout.minimumHeight: 12*3
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                //                Layout.fillHeight: true
                Layout.fillWidth: true
                model: publicModel
                textRole: "text"
            }

//            Rectangle{
//                Layout.preferredHeight: 40
//                Layout.maximumHeight: 60
//                Layout.row: 3
//                Layout.column: 0
//            }
            RowLayout {
                Layout.row: 4
                Layout.column: 1

                OrangeButton {
                    id: btn
                    text: "保存"
                    Layout.maximumHeight: 60
                    Layout.minimumWidth: 200
    //            Layout.columnSpan: 2

                //Layout.preferredWidth: 500


                    onClicked: {
                        if ( m_mode === 1 ) {
                            createDataset(dataset_name.text, dataset_path.text, dataset_description.text, publicModel.get(publicCombo.currentIndex).pb)
//                        client.setConfig(m_token)
//                        var res = client.create_dataset(dataset_name.text, dataset_path.text, dataset_description.text)
//                        var jres = JSON.parse(res)
//                        if ( jres["code"] === 0 ) {
//                            dataset_create.close()
//                            success()
//                            return
//                        }
//                        tip.showmsg(jres["message"])
                        } else if ( m_mode === 0 ) {
                            editDataset(m_uid, dataset_name.text, dataset_description.text)
//                        client.setConfig(m_token)
//                        var res = client.edit_dataset(m_uid, dataset_name.text, dataset_description.text)
//                        var jres = JSON.parse(res)
//                        if ( jres["code"] === 0 ) {
//                            dataset_create.close()
//                            success()
//                            return
//                        }
//                        tip.showmsg(jres["message"])
                        }
                    }
                }

                OrangeButton {
                    text: "返回"
                    Layout.maximumHeight: 60
                    Layout.minimumWidth: 200

                    onClicked: {
                        cancel()
                    }
                }

                Rectangle {
                    color: "transparent"
                    Layout.fillWidth: true
                }
            }

    //        TextEdit {
    //            id: content
    //            text: m_content
    //            width: 350
    //            Layout.preferredHeight: 500
    //        }
            Rectangle {
                Layout.row: 5
                Layout.column: 0
//                Layout.minimumHeight: 200
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "transparent"
                Layout.columnSpan: 3
            }
        }

    }




    Binding {
        target: dataset_create
        property: "m_dataset_name"
        value: dataset_name.text
    }
    Binding {
        target: dataset_name
        property: "text"
        value: dataset_create.m_dataset_name
    }
    Binding {
        target: dataset_create
        property: "m_dataset_description"
        value: dataset_description.text
    }
    Binding {
        target: dataset_description
        property: "text"
        value: dataset_create.m_dataset_description
    }
    Binding {
        target: dataset_create
        property: "m_dataset_path"
        value: dataset_path.text
    }
    Binding {
        target: dataset_path
        property: "text"
        value: dataset_create.m_dataset_path
    }
    Binding {
        target: dataset_create
        property: "m_btn"
        value: btn.text
    }
    Binding {
        target: btn
        property: "text"
        value: dataset_create.m_btn
    }
    Binding {
        target: publicCombo
        property: "currentIndex"
        value: m_pb
    }


//    Binding {
//        target: model_create
//        property: "m_model_class"
//        value: model_class.text
//    }
//    Binding {
//        target: model_class
//        property: "text"
//        value: model_create.m_model_class
//    }
//    Binding {
//        target: model_create
//        property: "m_dependencies"
//        value: dependencies.text
//    }
//    Binding {
//        target: dependencies
//        property: "text"
//        value: model_create.m_dependencies
//    }
//    Binding {
//        target: model_create
//        property: "m_content"
//        value: content.text
//    }
//    Binding {
//        target: content
//        property: "text"
//        value: model_create.m_content
//    }

//    signal success()
//    signal failed()
}
